package com.eedi.framework.permission.controller.sys;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.permission.controller.sys.vo.permission.SysPermissionAssignUserRoleReq;
import com.eedi.framework.permission.service.SysPermissionService;
import com.eedi.framework.permission.controller.sys.vo.permission.SysPermissionAssignRoleMenuReq;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

import static com.eedi.framework.common.pojo.CommonResult.success;

/**
 * 权限 Controller，提供赋予用户、角色的权限的 API 接口
 *
 * @author 永聚长青源码
 */
@Tag(name = "系统界面 - 权限")
@RestController
@RequestMapping("/permission")
public class SysPermissionController {

    @Resource
    private SysPermissionService sysPermissionService;


    @Operation(summary = "获得角色拥有的菜单编号")
    @Parameter(name = "sysRoleId", description = "角色编号", required = true)
    @GetMapping("/list-role-menus")
    @PreAuthorize("@sys.hasPermission('sys:permission:assign-role-menu')")
    public CommonResult<Set<String>> getRoleMenuList(String sysRoleId) {
        return success(sysPermissionService.getRoleMenuListByRoleId(sysRoleId));
    }

    @PostMapping("/assign-role-menu")
    @Operation(summary = "赋予角色菜单")
    @PreAuthorize("@sys.hasPermission('sys:permission:assign-role-menu')")
    public CommonResult<Boolean> assignRoleMenu(@Validated @RequestBody SysPermissionAssignRoleMenuReq reqVO) {

        // 执行菜单的分配
        sysPermissionService.assignRoleMenu(reqVO.getSysRoleId(), reqVO.getSysMenuIds());
        return success(true);
    }

    @PostMapping("/remove-role-menu")
    @Operation(summary = "移除角色菜单")
    @PreAuthorize("@sys.hasPermission('sys:permission:assign-role-menu')")
    public CommonResult<Boolean> removeRoleMenu(@Validated @RequestBody SysPermissionAssignRoleMenuReq reqVO) {

        // 执行菜单的分配
        sysPermissionService.assignRoleMenu(reqVO.getSysRoleId(), reqVO.getSysMenuIds());
        return success(true);
    }

//    @PostMapping("/assign-role-data-scope")
//    @Operation(summary = "赋予角色数据权限")
//    @PreAuthorize("@sys.hasPermission('sys:permission:assign-role-data-scope')")
//    public CommonResult<Boolean> assignRoleDataScope(@Valid @RequestBody SysPermissionAssignRoleDataScopeReq reqVO) {
//        sysPermissionService.assignRoleDataScope(reqVO.getSysRoleId(), reqVO.getSysRoleDataScope(), reqVO.getSysRoleDataScopeDeptIds());
//        return success(true);
//    }

    @Operation(summary = "获得管理员拥有的角色编号列表")
    @Parameter(name = "sysUserId", description = "用户编号", required = true)
    @GetMapping("/list-user-roles")
    @PreAuthorize("@sys.hasPermission('sys:permission:assign-user-role')")
    public CommonResult<Set<String>> listAdminRoles(@RequestParam("sysUserId") String sysUserId) {
        return success(sysPermissionService.getUserRoleIdListByUserId(sysUserId));
    }

    @Operation(summary = "赋予用户角色")
    @PostMapping("/assign-user-role")
    @PreAuthorize("@sys.hasPermission('sys:permission:assign-user-role')")
    public CommonResult<Boolean> assignUserRole(@Validated @RequestBody SysPermissionAssignUserRoleReq reqVO) {
        sysPermissionService.assignUserRole(reqVO.getSysUserId(), reqVO.getSysRoleIds());
        return success(true);
    }

    @PutMapping("/count-admin")
    @Operation(summary = "租户管理员数量")
    @PreAuthorize("@sys.hasPermission('sys:permission:count-admin')")
    public CommonResult<Long> countAdmin() {
        return success(sysPermissionService.countAdmin());
    }

}
